package com.hanagent.controller;

import com.hanagent.model.Agents;
import com.hanagent.model.Orders;
import com.hanagent.utils.ConstantUtils;
import com.hanagent.utils.ResponseUtils;
import com.hanagent.utils.StrUtils;
import com.hanagent.utils.UUidUtils;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

public class OrdersController extends Controller{
	
	public void getList() {
		int pageNumber = getParaToInt("pageNumber")<1?1:getParaToInt("pageNumber");
		int pageSize = getParaToInt("pageSize")<1?1:getParaToInt("pageSize");
		String startCreateTime = getPara("startCreateTime");
		if (StrUtils.isEmpty(startCreateTime)) {
			startCreateTime = "1917-12-05T16:00:00.000Z";
		} 
		String endCreateTime = getPara("endCreateTime");
		if (StrUtils.isEmpty(endCreateTime)) {
			endCreateTime = "3017-12-05T16:00:00.000Z";
		}  
		String startUpdateTime = getPara("startUpdateTime");
		if (StrUtils.isEmpty(startUpdateTime)) {
			startUpdateTime = "1917-12-05T16:00:00.000Z";
		} 
		String endUpdateTime = getPara("endUpdateTime");
		if (StrUtils.isEmpty(endUpdateTime)) {
			endUpdateTime = "3017-12-05T16:00:00.000Z";
		} 
		
		String agentId = getPara("agentId");
		Agents loginAgents = getAttr("loginAgents"); //登陆的代理商
		
		if (StrUtils.isEmpty(agentId)) {
			if (loginAgents.getAgentLevel()>ConstantUtils.LevelCompanySource){
				Page<Orders> list = Orders.dao.paginate(pageNumber, pageSize, "select * ", "from Orders where createdAt > ? and "
						+ "createdAt < ? and updatedAt > ? and updatedAt < ? and agentId = ? order by updatedAt desc",
						startCreateTime, endCreateTime, startUpdateTime, endUpdateTime, loginAgents.getAgentId());
		        renderText(ResponseUtils.sendResult(1, list, "获取数据成功"));
			}else{
				Page<Orders> list = Orders.dao.paginate(pageNumber, pageSize, "select * ", "from Orders where createdAt > ? and "
						+ "createdAt < ? and updatedAt > ? and updatedAt < ? order by updatedAt desc",
						startCreateTime, endCreateTime, startUpdateTime, endUpdateTime);
		        renderText(ResponseUtils.sendResult(1, list, "获取数据成功"));
			}
		}else{
			if (loginAgents.getAgentLevel()>ConstantUtils.LevelCompanySource && !agentId.equals(loginAgents.getAgentId()+"")) {
				renderText(ResponseUtils.sendResult(0, "", "你无权查看其他代理商的顾客订单"));
				return;
			}
			Page<Orders> list = Orders.dao.paginate(pageNumber, pageSize, "select * ", "from Orders where createdAt > ? and "
					+ "createdAt < ? and updatedAt > ? and updatedAt < ? and agentId = ? order by updatedAt desc",
					startCreateTime, endCreateTime, startUpdateTime, endUpdateTime, agentId);
	        renderText(ResponseUtils.sendResult(1, list, "获取数据成功"));
		}
    }
	
	/**  
     */  
    public void addOrders() {  
    	Orders orders = getModel(Orders.class, "");//别名  Jfinal 必杀技快速开发全靠它  
    	orders.setOrderId(UUidUtils.getUUid());
    	if (StrUtils.isEmpty(orders.getPayImg())) { //非未支付订单必须上传支付凭证
    		renderText(ResponseUtils.sendResult(0, "", "必须上传支付凭证")); 
    		return;
		}

    	Agents loginAgents = getAttr("loginAgents"); //登陆的代理商  设置该订单是否可分红的标志
    	if (loginAgents.getAgentLevel()<ConstantUtils.LevelTop) {
			orders.setShareState(0);
		}else{
			orders.setShareState(2);
		}
    	
        boolean flag = orders.save();  
        if (flag) {  
        	renderText(ResponseUtils.sendResult(1, "", "添加成功"));  
        }else {  
        	renderText(ResponseUtils.sendResult(0, "", "添加失败"));  
        }  
    }

    /** 
     * 修改 
     */   
    public void updateOrders() {  
    	Orders orders = getModel(Orders.class, "");//别名  Jfinal 必杀技快速开发全靠它  
        boolean flag = orders.update();
        if (flag) {  
        	renderText(ResponseUtils.sendResult(1, "", "修改成功"));  
        }else {  
        	renderText(ResponseUtils.sendResult(0, "", "修改失败"));  
        }   
    }  
      
    /** 
     * 删除单个 
     */  
    public void delOrdersById() {  
        Integer id = getParaToInt("orderId");  
        boolean flag = Orders.dao.deleteById(id);  
        if (flag) {  
        	renderText(ResponseUtils.sendResult(1, "", "删除成功"));  
        }else {  
        	renderText(ResponseUtils.sendResult(0, "", "删除失败"));  
        }    
    } 

}
